package com.example.qundui.learndemo.rv;
import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qundui.learndemo.R;
import com.example.qundui.learndemo.lvtest.MyAdapter;
import com.example.qundui.learndemo.lvtest.MyUtils;
import com.example.qundui.learndemo.lvtest.bases;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qundui on 2017/5/5.
 */
public class RvTest01 extends bases {
    private RecyclerView rv;
    private MyAdater myAdater;
    private List<String> mDatas2;
    List<String> mDatas;

    @Override
    protected void doBusy() {
        rv= (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
         mDatas=new ArrayList<>();
        for(int i=0;i<10;i++){
            mDatas.add("第"+(i+1)+"条数据");

        }
        myAdater=new MyAdater(mDatas,this);
        rv.setAdapter(myAdater);

    }
    @Override
    protected int getLayout() {
        return R.layout.test01;
    }
public void resfush(View v){
     mDatas2=new ArrayList<>();
    for(int i=0;i<18;i++){
        mDatas2.add("第"+(i+1)+"条数据");

    }
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyUtils(mDatas, mDatas2), true);
    mDatas=mDatas2;
   myAdater.setMdata(mDatas);
    diffResult.dispatchUpdatesTo(myAdater);
    Toast.makeText(this,"resfush",Toast.LENGTH_SHORT).show();

}
    class MyAdater extends RecyclerView.Adapter<VH1>{
        List<String> mdata;
        Context ctx;
        public List<String> getMdata() {
            return mdata;
        }

        public void setMdata(List<String> mdata) {
            this.mdata = mdata;
        }

        public MyAdater(List<String> mdata, Context ctx) {
            this.mdata = mdata;
            this.ctx = ctx;
        }
        @Override
        public VH1 onCreateViewHolder(ViewGroup parent, int viewType) {
            return new VH1(LayoutInflater.from(ctx).inflate(R.layout.rv_item_test,parent,false));
        }
        @Override
        public void onBindViewHolder(VH1 holder, int position) {
            holder.tv.setText(mdata.get(position));
        }
        @Override
        public int getItemCount() {
            return mdata.size();
        }
    }
    private class VH1 extends RecyclerView.ViewHolder{
        private TextView tv;
        public VH1(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
